package com.lusang.gift.controller;

import com.lusang.gift.dto.TenantEnterDto;
import com.lusang.gift.service.ITenantService;
import com.lusang.gift.domain.Tenant;
import com.lusang.gift.query.TenantQuery;
import com.lusang.gift.util.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    public ITenantService tenantService;


    /**
     * 保存和修改公用的
     * @param tenant  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Tenant tenant){
        try {
            if( tenant.getId()!=null)
                tenantService.updateById(tenant);
            else
                tenantService.insert(tenant);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            tenantService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {

            Tenant tenant = tenantService.selectById(id);
            return AjaxResult.me().setResultObj(tenant);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Tenant> list = tenantService.selectList(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody TenantQuery query)
    {
        //高级查询1：使用wrapper做高级查询+分页 非关联表查询
        //通过wrapper设置条件
        /*EntityWrapper<Tenant> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(query.getKeyword())){//不是空白
            wrapper.like("company_name", query.getKeyword())
                    .or()
                    .like("address", query.getKeyword());
        }
        Page<Tenant> page = new Page<Tenant>(query.getPage(),query.getRows());
        page = tenantService.selectPage(page,wrapper);
        PageList pageList = new PageList<Tenant>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);*/
        //高级查询2：自己写sql 高级+分页+关联
        return AjaxResult.me().setResultObj(tenantService.selectPageList(query));
    }

    @PostMapping("/settlement")
    public AjaxResult settlement( @RequestBody @Valid
                                          TenantEnterDto tenantEnterDto){
        /*try {
            return tenantService.settlement(tenantEnterDto);
        }
        catch (BusinessException e){
            //可以预知错误
            log.error("Error occurred！"+e.getMessage());
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
        catch (Exception e) {
            //不可预知错误
            //e.printStackTrace();
            log.error("Error occurred！"+e.getMessage());
            return AjaxResult.me().setSuccess(false).setMessage("系统异常，请稍后重试！");
        }*/
        return tenantService.settlement(tenantEnterDto);
    }
}
